# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=tools
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=12.0.0.r747.g1a99f8514
pkgrel=1
_commit='1a99f85148242b15b5f1fec1d4a318c1869a5816'
pkgdesc="MinGW-w64 tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.mingw-w64.org/"
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-tools/"
license=('spdx:GPL-3.0-or-later AND LGPL-2.1-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-libmangle"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('b1f7f5b22a61a03cafc9d9f6b211451bd9651e3395b1897d2fd52b93b5f1d26c')
_tools="gendef genidl genpeimg widl" # genstubdll

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

build() {
  for cur in ${_tools}; do
    plain "Building ${cur} ..."
    mkdir -p "${srcdir}"/${MSYSTEM}-${cur} && cd "${srcdir}"/${MSYSTEM}-${cur}
    "${srcdir}"/mingw-w64/mingw-w64-tools/${cur}/configure \
      --prefix=${MINGW_PREFIX} \
      --with-mangle=${MINGW_PREFIX} \
      --with-widl-includedir=${MINGW_PREFIX}/include
    make
  done
}

package() {
  for cur in ${_tools}; do
    plain "Installing ${cur} ..."
    cd "${srcdir}"/${MSYSTEM}-${cur}
    make DESTDIR="${pkgdir}" install
    if [[ -f "${srcdir}/mingw-w64/mingw-w64-tools/${cur}"/COPYING ]]; then
      install -Dm644 "${srcdir}"/mingw-w64/mingw-w64-tools/${cur}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/mingw-w64-tools/COPYING.${cur}
    fi
  done
}
